import React, { Component } from "react";
import SideBarUnit from "./common/comps/sideBarUnit";
import "../css/Sidebar.css";
import hmsUrlHook from "./common/utils/urlHook";
import CookieUtils from "pages/ruijing/common/utils/CookieUtils";
import urlHook from "pages/ruijing/common/utils/urlHook";
import UserApi from "@module/userApi";
import HttpUtils from "pages/ruijing/common/utils/HttpUtils";

const orgId = CookieUtils.getCookie("orgId");

function Bar(props) {
	const selectedGroup = StateApi.getStoreState("hmsMenuConfig").selectedGroup;
	let permissions = JSON.parse(localStorage.getItem("STORE_USERPERMISSION"));
	const allTabLength = 14;
    const iconClass = ["purchase-nav-icon","bidding-nav-icon","order-nav-icon","account-nav-icon","commodity-nav-icon","brand-nav-icon","provider-nav-icon"];
    const iconActiveClass = ["purchase-nav-icon-active","bidding-nav-icon-active","order-nav-icon-active","account-nav-icon-active","commodity-nav-icon-active","brand-nav-icon-active","provider-nav-icon-active"];
    let showGroup = [];
    for(let i=0;i<allTabLength;i++){
        showGroup.push(true);
    }
    const [menuStatus, updateMenuStatus] = React.useState({
        showGroup: showGroup,
        selectedGroup: selectedGroup,
        permissions: permissions
    });

    //更新视图数据 私有的方法前缀用_
    function _updateData(value) {
        let newValue = { ...menuStatus, ...value };
        updateMenuStatus(newValue);
        //(updateMenuStatus(newValue),() =>renderMenu())

    }

    function updateSelected(selectGroup){
        _updateData(
            (menuStatus.selectedGroup = selectGroup)
        )
    }

    //自动登出方法
    function logout() {
        ZenUI.ST("请先登录", 2);
        CookieUtils.clearAllCookiesAndStorage();
        setTimeout(() => {
            Uts.routeToDomain('hms','')
        }, 500);
    }


    React.useEffect(() => {
    });
    if(CookieUtils.getCookie("orgId")&&CookieUtils.getCookie("orgId")!=undefined&&CookieUtils.getCookie("orgId")!=null&&permissions&&permissions.length>0&&permissions.indexOf("HMS用户")>-1) {
        return (
            <div className="zen_f-left">
                <ul className="zen_w-180 zen_bg-293038 zen_elli" style={{"minHeight": "600px", "height": "1200px"}}>
                    <li className="zen_w-180 zen_h-30">
                        <div className="nav-show-hide-icon"></div>
                    </li>
                    {SimplePermitTab(0, "采购管理", ["采购单查看"],[], hmsUrlHook.purchaseTab, menuStatus, updateSelected, "purchase-nav-icon", "purchase-nav-icon-active")}
                    {SimplePermitTab(1, "竞价管理", ["竞价查看"],[36], hmsUrlHook.bidTab, menuStatus, updateSelected, "bidding-nav-icon", "bidding-nav-icon-active")}
                    {SimplePermitTab(2, "订单管理", ["订单查看"],[], hmsUrlHook.orderTab, menuStatus, updateSelected, "order-nav-icon", "order-nav-icon-active")}
                    {SimplePermitTab(3, "结算管理", ["结算单查看"],[], hmsUrlHook.accountTab, menuStatus, updateSelected, "account-nav-icon", "account-nav-icon-active")}
                    {SimplePermitTab(5, "品牌管理", ["品牌列表"],[], hmsUrlHook.brandTab, menuStatus, updateSelected, "brand-nav-icon", "brand-nav-icon-active")}
                    {SimplePermitTab(6, "供应商管理", ["供应商查看"],[], hmsUrlHook.supplierTab, menuStatus, updateSelected, "provider-nav-icon", "provider-nav-icon-active")}
                    {SimplePermitTab(7, "出入库管理", ["出入库查看","出入库审批"],[], hmsUrlHook.warehouseTab, menuStatus, updateSelected, "storage-nav-icon", "storage-nav-icon-active")}
                    {renderGarbageTab(13, "废弃物管理", ["废弃物申请"],[], hmsUrlHook.garbageTab, menuStatus, updateSelected, "order-nav-icon", "order-nav-icon-active")}
                    {renderMemberTab(8, "成员管理", ["人员查看"],[], hmsUrlHook.memberTab, menuStatus, updateSelected, "human-nav-icon", "human-nav-icon-active")}
                    {renderStatisticTab(9, "统计管理", ["统计查看","医院统计查看"],[], hmsUrlHook.statisticTab, menuStatus, updateSelected, "statistic-nav-icon", "statistic-nav-icon-active")}
                    {SimplePermitTab(10, "采购管控", ["预警记录查看","预警设置查看","预警设置编辑"],[], hmsUrlHook.monitorTab, menuStatus, updateSelected, "statistic-nav-icon", "statistic-nav-icon-active")}
                    {SimplePermitTab(11, "系统管理", ["地址标签管理"],[3], "/systemSet", menuStatus, updateSelected, "system-nav-icon", "system-nav-icon-active")}
                    {RenderPersonalTab(12, "个人中心", [""],[], hmsUrlHook.personalTab, menuStatus, updateSelected, "personal-nav-icon", "personal-nav-icon-active")}
                </ul>
            </div>
        );
    }else{
        return ""
    }
}

function allIncludes(arr1, arr2) {
    return arr2.every(val => arr1.includes(val));
}

//由权限值以及医院黑名单所构成的侧边栏权限简单逻辑
function SimplePermitTab(tabid,name,tabPermission,refuseOrg,taburl,menuStatus,updataFunction,iconClass,iconActiveClass) {
    if(menuStatus.permissions.find(item =>tabPermission.includes(item))&&refuseOrg.indexOf(orgId)==-1){
        return <SideBarUnit
            name = {name}
            taburl = {taburl}
            tabid = {tabid}
            updateMenu = {updataFunction}
            menuStatus = {menuStatus}
            iconClass = {iconClass}
            iconActiveClass = {iconActiveClass}
        />
    }
}

//复杂逻辑构成的废弃物管理权限
function renderGarbageTab(tabid,name,tabPermission,refuseOrg,taburl,menuStatus,updataFunction,iconClass,iconActiveClass) {
    if(menuStatus.permissions.find(item =>tabPermission.includes(item))&&refuseOrg.indexOf(orgId)==-1){
        if((sessionStorage.getItem("USERSTORAGE_CONFIG_GARBAGE") != null && sessionStorage.getItem("USERSTORAGE_CONFIG_GARBAGE") == "true") || sessionStorage.getItem("USERSTORAGE_CONFIG_GARBAGE") == null) {
            return <SideBarUnit
                name = {name}
                taburl = {taburl}
                tabid = {tabid}
                updateMenu = {updataFunction}
                menuStatus = {menuStatus}
                iconClass = {iconClass}
                iconActiveClass = {iconActiveClass}
            />
        }
    }
}

//复杂逻辑构成的统计管理权限
function renderStatisticTab(tabid,name,tabPermission,refuseOrg,taburl,menuStatus,updataFunction,iconClass,iconActiveClass) {
    if(menuStatus.permissions.find(item =>tabPermission.includes(item))&&refuseOrg.indexOf(orgId)==-1){
        let memberUrl = taburl;
        if(menuStatus.permissions.includes("医院统计查看")){
            memberUrl = "statistic";
        }else if(menuStatus.permissions.includes("统计查看")){
            memberUrl = "departDetail";
        }
        return <SideBarUnit
            name = {name}
            taburl = {memberUrl}
            tabid = {tabid}
            updateMenu = {updataFunction}
            menuStatus = {menuStatus}
            iconClass = {iconClass}
            iconActiveClass = {iconActiveClass}
        />
    }
}

//复杂逻辑构成的成员管理权限
function renderMemberTab(tabid,name,tabPermission,refuseOrg,taburl,menuStatus,updataFunction,iconClass,iconActiveClass) {
    if(menuStatus.permissions.find(item =>tabPermission.includes(item))&&refuseOrg.indexOf(orgId)==-1){
        let memberUrl = taburl;
        if(menuStatus.permissions.includes("供应商医院审批")){
            memberUrl = "member";
        }else if(menuStatus.permissions.includes("权限查看")){
            memberUrl = "roles";
        }else if(menuStatus.permissions.includes("部门查看")){
            memberUrl = "department";
        }
        if(menuStatus.permissions.find(item =>["供应商医院审批","权限查看","部门查看"].includes(item))){
            return <SideBarUnit
                name = {name}
                taburl = {memberUrl}
                tabid = {tabid}
                updateMenu = {updataFunction}
                menuStatus = {menuStatus}
                iconClass = {iconClass}
                iconActiveClass = {iconActiveClass}
            />
        }
    }
}
//无权限限制的个人中心
function RenderPersonalTab(tabid,name,tabPermission,refuseOrg,taburl,menuStatus,updataFunction,iconClass,iconActiveClass) {
    return <SideBarUnit
        name = {name}
        taburl = {taburl}
        tabid = {tabid}
        updateMenu = {updataFunction}
        menuStatus = {menuStatus}
        iconClass = {iconClass}
        iconActiveClass = {iconActiveClass}
    />
}

export default Bar;
